package com.fangjinwang.zhushunqing.Ui;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;

import com.fangjinwang.zhushunqing.App.AppConstant;
import com.fangjinwang.zhushunqing.Bean.HomeTabEntity;
import com.fangjinwang.zhushunqing.R;
import com.fangjinwang.zhushunqing.Ui.Help.HelpFragment;
import com.fangjinwang.zhushunqing.Ui.Home.HomeFragment;
import com.fangjinwang.zhushunqing.Ui.Product.ProductFragment;
import com.fangjinwang.zhushunqing.Ui.Wallet.WalletFragment;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.jaydenxiao.common.base.BaseActivity;
import com.jaydenxiao.common.commonutils.LogUtils;

import java.util.ArrayList;

import butterknife.Bind;

/**
 * Created by admin on 2016/11/3.
 */
public class MainActivity extends BaseActivity {
    @Bind(R.id.tab_layout)
    CommonTabLayout tabLayout;

    private HomeFragment homeFragment;
    private ProductFragment productFragment;

    private WalletFragment walletFragment;
    private HelpFragment helpFragment;

    private ArrayList<CustomTabEntity> tabList = new ArrayList<CustomTabEntity>();

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void initView() {
        SetTranslanteBar();
//        SetStatusBarColor(ContextCompat.getColor(this, com.jaydenxiao.common.R.color.white));
        initTab();

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initFragment(savedInstanceState);
    }

    private void initFragment(Bundle savedInstanceState) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        int currentTabPosition = 0;
        if (savedInstanceState != null) {
            homeFragment = (HomeFragment) getSupportFragmentManager().findFragmentByTag("HomeFragment");
            productFragment = (ProductFragment) getSupportFragmentManager().findFragmentByTag("ProductFragment");
            walletFragment = (WalletFragment) getSupportFragmentManager().findFragmentByTag("WalletFragment");
            helpFragment = (HelpFragment) getSupportFragmentManager().findFragmentByTag("HelpFragment");
            currentTabPosition = savedInstanceState.getInt(AppConstant.HOME_CURRENT_TAB_POSITION);
        } else {
            homeFragment = new HomeFragment();
            productFragment = new ProductFragment();
            walletFragment = new WalletFragment();
            helpFragment = new HelpFragment();

            transaction.add(R.id.fl_body, homeFragment, "HomeFragment");
            transaction.add(R.id.fl_body, productFragment, "ProductFragment");
            transaction.add(R.id.fl_body, walletFragment, "WalletFragment");
            transaction.add(R.id.fl_body, helpFragment, "HelpFragment");
        }
        transaction.commit();
        SwitchTo(currentTabPosition);
        tabLayout.setCurrentTab(currentTabPosition);
    }

    private void initTab() {
        tabList.add(new HomeTabEntity("首页", R.mipmap.tab_index_selected, R.mipmap.tab_index_default));
        tabList.add(new HomeTabEntity("理财产品", R.mipmap.tab_product_selected, R.mipmap.tab_product_default));
        tabList.add(new HomeTabEntity("我的钱包", R.mipmap.tab_wallet_selected, R.mipmap.tab_wallet_default));
        tabList.add(new HomeTabEntity("帮助", R.mipmap.tab_helf_selected, R.mipmap.tab_help_default));
        tabLayout.setTabData(tabList);

        //点击监听
        tabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                SwitchTo(position);
            }

            @Override
            public void onTabReselect(int position) {
            }
        });
    }

    /**
     * 切换
     *
     * @param position
     */
    private void SwitchTo(int position) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        switch (position) {
            //首页
            case 0:
                transaction.hide(productFragment);
                transaction.hide(helpFragment);
                transaction.hide(walletFragment);
                transaction.show(homeFragment);
                transaction.commitAllowingStateLoss();
                break;
            //美女
            case 1:
                transaction.show(productFragment);
                transaction.hide(helpFragment);
                transaction.hide(walletFragment);
                transaction.hide(homeFragment);
                transaction.commitAllowingStateLoss();
                break;
            //视频
            case 2:
                transaction.hide(productFragment);
                transaction.hide(helpFragment);
                transaction.show(walletFragment);
                transaction.hide(homeFragment);
                transaction.commitAllowingStateLoss();
                break;
            //关注
            case 3:
                transaction.hide(productFragment);
                transaction.show(helpFragment);
                transaction.hide(walletFragment);
                transaction.hide(homeFragment);
                transaction.commitAllowingStateLoss();
                break;
            default:
                break;
        }
    }

//    @Override
//    protected void onTintStatusBar(int color) {
//        if (mStatusBarHelper == null) {
//            mStatusBarHelper = new StatusBarHelper(this, StatusBarHelper.LEVEL_19_TRANSLUCENT,
//                    StatusBarHelper.LEVEL_21_NORMAL_FULL);
//        }
//        mStatusBarHelper.setActivityRootLayoutFitSystemWindows(false);
//        mStatusBarHelper.setColor(color);
//    }



    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //奔溃前保存位置
        LogUtils.loge("onSaveInstanceState进来了1");
        if (tabLayout != null) {
            LogUtils.loge("onSaveInstanceState进来了2");
            outState.putInt(AppConstant.HOME_CURRENT_TAB_POSITION, tabLayout.getCurrentTab());
        }
    }
}
